package com.woniuxy.redis01;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.redis01.entity.Student;
import org.junit.jupiter.api.Test;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

@SpringBootTest
class Redis01ApplicationTests02 {

    @Autowired
    RedissonClient redissonClient;

    @Test
    void test01()  {
        RLock lock = redissonClient.getLock("lock");
        //  lock  (key ,1)
        lock.lock();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("test01");
        test02();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        //del
        lock.unlock();
    }

    private void test02() {
        //02这个方法要使用01方法的锁，看他是不是可重入锁
        //  lock  (key ,2)
        RLock lock = redissonClient.getLock("lock");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        lock.lock();
        System.out.println("test02");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        //  lock  (key ,1)
        lock.unlock();

    }

}
